//
//  ReadViewController.m
//  Comments
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//
#import "BaseHeader.h"
#import "ReadViewController.h"
#import "commentViewController.h"
#import "ZYLFastlookAllContentViewController.h"
#import "AFNetworkHandler.h"
#import "LSYReadScroView.h"
#import "LSYHotCommentsTableViewCell.h"
#import "ZYLImageSize.h"
#import "LSYFastLookImagesTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface ReadViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UIView *downView;
@property (nonatomic, retain) NSDictionary *readDataDic;
@property (nonatomic, retain) NSArray *imagesArr;
@property (nonatomic, retain) LSYReadScroView *imagesScro;
@property (nonatomic, retain) NSString *titleStr;

@property (nonatomic, retain) UITableView *commentsTableView;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, retain) NSMutableArray *commentsDataArr;
@property (nonatomic, assign) CGFloat oldContentset_Y;
@property (nonatomic, retain) UIView *headerBackgroundView;
@property (nonatomic, retain) UIView *myNavigationBackView;
@end

@implementation ReadViewController
- (void)dealloc
{
    _commentsTableView.delegate = nil;
    [_commentsTableView release];
    [_commentsDataArr release];
    [_headerBackgroundView release];
    [_myNavigationBackView release];
    [_titleStr release];
    [_imagesScro release];
    [_readDataDic release];
    [_imagesArr release];
    [_ID release];
    [_downView release];
    [super dealloc];
}
- (void)viewDidLoad {
    self.automaticallyAdjustsScrollViewInsets = NO;
    [super viewDidLoad];
    self.oldContentset_Y = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    
}



#pragma mark - 热门评论 TableView
- (void)createCommentsTableView
{
    self.commentsTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped] autorelease];
    self.commentsTableView.delegate = self;
    self.commentsTableView.dataSource = self;
    self.commentsTableView.bounces = NO;
    self.commentsTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
    self.commentsTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.commentsTableView];
    [self createMyNavigationBar];
    [self createFooterView];
    
}
#pragma mark - tableView协议方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        LSYFastLookImagesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"l"];
        if (!cell) {
            cell = [[LSYFastLookImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"l"];
        }
//        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.separatorColor = [UIColor clearColor];
        cell.imagesUrl = self.imagesArr[indexPath.row];
        return cell;
    }
    
    LSYHotCommentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"s"];
    if (!cell) {
        cell = [[LSYHotCommentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"s"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.commentsDataArr[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == section) {
        return self.imagesArr.count;
    }
    return self.commentsDataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        CGFloat h = [LSYFastLookImagesTableViewCell imageHeight:self.imagesArr[indexPath.row]].height;
        CGFloat w = [LSYFastLookImagesTableViewCell imageHeight:self.imagesArr[indexPath.row]].width;
        CGFloat height = SCREEN_WIDTH * h / w;
        return height;
    }
    
    LSYFastLookHotCommentsModel *model = self.commentsDataArr[indexPath.row];
    CGFloat h = [LSYHotCommentsTableViewCell heightWithText:model.content];
    self.height = h + 70 * SCREEN_HEIGHT / 667;
    return self.height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (1 == section) {
        LSYReadScroView *sectionView = [LSYReadScroView readScroViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) rdataDic:self.readDataDic];
        return sectionView;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (1 == section) {
        return 150;
    }
    return 0;
}
/**
 *  导航栏
 */
- (void)createMyNavigationBar
{
    self.myNavigationBackView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)] autorelease];
    self.myNavigationBackView.tag = 12000;
    self.myNavigationBackView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myNavigationBackView];
    UIView *line = [[[UIView alloc] initWithFrame:CGRectMake(0, 63.5 * SCREEN_HEIGHT / 667, SCREEN_WIDTH, 0.5)] autorelease];
    line.backgroundColor = [UIColor lightGrayColor];
    [self.myNavigationBackView addSubview:line];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(buttonBack) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(8 * SCREEN_WIDTH / 375, 27 * SCREEN_HEIGHT / 667, 30  * SCREEN_WIDTH / 375, 30  * SCREEN_WIDTH / 375);
    [self.myNavigationBackView addSubview:back];
    CGSize titleSize = [self.titleStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size;
    UILabel *naviTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleSize.width, 30 * SCREEN_HEIGHT / 667)] autorelease];
    naviTitle.text = self.titleStr;
    naviTitle.textAlignment = NSTextAlignmentCenter;
    naviTitle.font = [UIFont systemFontOfSize:17];
    naviTitle.center = CGPointMake(SCREEN_WIDTH / 2.0, back.center.y);
    [self.myNavigationBackView addSubview:naviTitle];
    
    UIButton *allContentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    allContentButton.frame = CGRectMake(SCREEN_WIDTH - back.frame.origin.x - 40 * SCREEN_WIDTH / 375, back.frame.origin.y, 40 * SCREEN_WIDTH / 375, 20 * SCREEN_HEIGHT / 667);
    [allContentButton setTitle:@"专题" forState:UIControlStateNormal];
    allContentButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [allContentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.myNavigationBackView addSubview:allContentButton];
    [allContentButton addTarget:self action:@selector(goAllcontentAction) forControlEvents:UIControlEventTouchUpInside];
}

/**
 *  最底部 查看更过评论
 */
- (void)createFooterView
{
    UIButton *moreCommentsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    moreCommentsButton.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20 * SCREEN_HEIGHT / 667);
    [moreCommentsButton setTitle:@"查看更多评论" forState:UIControlStateNormal];
    moreCommentsButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [moreCommentsButton addTarget:self action:@selector(moreCommentAction) forControlEvents:UIControlEventTouchUpInside];
    [moreCommentsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.commentsTableView.tableFooterView = moreCommentsButton;
}

- (void)buttonBack
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - scroView 协议方法 控制隐藏 Navi
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIView *view = [self.view viewWithTag:12000];
    CGFloat f = scrollView.contentOffset.y - self.oldContentset_Y;
    self.oldContentset_Y = scrollView.contentOffset.y;
    if (f > 5) {
        [UIView animateWithDuration:0.5 animations:^{
            CGRect f = view.frame;
            f.origin.y = -64;
            view.frame = f;
        }];
    }
    if (f < -10) {
        [UIView animateWithDuration:0.5 animations:^{
            CGRect f = view.frame;
            f.origin.y = 0;
            view.frame = f;
        }];
    }
}


#pragma nark - 数据处理
- (void)getData
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        
        NSString *urlStr = @"http://api.kuaikanmanhua.com/v1/comics/";
        NSString *URL = [urlStr stringByAppendingString:self.ID.stringValue];
        [AFNetworkHandler GETwithAFNByURL:URL completion:^(id result) {
            self.readDataDic = [result objectForKey:@"data"];
            self.imagesArr = [self.readDataDic objectForKey:@"images"];
            self.titleStr = [self.readDataDic objectForKey:@"title"];
            if (self.commentsTableView == nil) {
                [self createCommentsTableView];
            }
        }];
        NSString *str = [@"http://api.kuaikanmanhua.com/v1/comics/" stringByAppendingString:self.ID.stringValue];
        NSString *HotcommentURL = [str stringByAppendingString:@"/hot_comments"];
        [AFNetworkHandler GETwithAFNByURL:HotcommentURL completion:^(id result) {
            NSDictionary *tempDic = [result objectForKey:@"data"];
            NSArray *arr = [tempDic objectForKey:@"comments"];
            self.commentsDataArr = [LSYFastLookHotCommentsModel arrayWithModelByArray:arr];
            if (self.commentsTableView != nil) {
                [self.commentsTableView reloadData];
            }
        }];
    });
}

/**
 *  更多评论 按钮
 */
- (void)moreCommentAction
{
    NSLog(@"dsaadsa");
    commentViewController *commentVC = [[[commentViewController alloc] init] autorelease];
    //    commentVC.hidesBottomBarWhenPushed = YES;
    commentVC.ID = self.ID;
    [self.navigationController pushViewController:commentVC animated:YES];
    
}

/**
 *  返回按钮绑定方法
 *
 *  @param animated backButton
 */
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 *  跳转专题界面 方法
 */
- (void)goAllcontentAction
{
    ZYLFastlookAllContentViewController *allContentVC = [[ZYLFastlookAllContentViewController alloc] init];
    allContentVC.ID = self.ID;
    [self.navigationController pushViewController:allContentVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
